package cn.itcast.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.*;
import cn.itcast.entity.Result;
import cn.itcast.pojo.*;
import cn.itcast.service.OrderService;
import cn.itcast.util.IdWorker;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.MemberDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderService;
import cn.itcast.util.IdWorker;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSetDao orderSetDao;

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private AddressDao addressDao;

    /**
     * 提交预约数据
     * @param order
     * @return
     */
    @Override
    public Result submit(Order order) {
        //判断当前用户是否预约过该套餐(根据身份证和套餐id来确定)
        String idCard = order.getIdCard();  //获取身份证id
        Integer setmealId = order.getSetmealId();   //获取套餐id

        Order result = this.orderDao.findByMid(idCard, setmealId);
        if (ObjectUtil.isNotEmpty(result)) {
            //之前预定过，不能继续预定该套餐
            return new Result(false, MessageConstant.HAS_ORDERED);
        }

        //判断当天是否可预约
        OrderSetting orderSetting = this.orderSetDao.findByDate(order.getOrderDate());
        if (ObjectUtil.isEmpty(orderSetting)) {
            //当天不可预约
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if (orderSetting.getNumber() - orderSetting.getReservations() == 0) {
            //预约已满
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //预约成功，当天已预约人数 + 1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        this.orderSetDao.updateReservationsNum(orderSetting);

        //保存数据
        order.setId(IdUtil.simpleUUID());
        order.setArrival(0);
        order.setSex(order.getSex());
        order.setOrderStatus(0);
        order.setPayStatus(0);  //未支付

        this.orderDao.addOrder(order);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }
    /**
     * 查询预约成功的信息
     * @param id
     * @return
     */
    @Override
    public Order findOrder(String id) {
        //查询mysql
        Order order = this.orderDao.findById(id);

        //根据套餐的id 查询套餐的名字
        Integer setmealId = order.getSetmealId();
        Setmeal setmeal = this.setmealDao.findById(setmealId);
        order.setSetmeal(setmeal);

        //根据分院地址id查询地址名字
        Integer addressId = order.getAddressId();
        Address address = this.addressDao.findById(addressId);
        order.setAddress(address);
        return order;
    }
    /**
     * 修改支付状态
     * @param orderId
     */
    @Override
    public void updatePayStatus(String orderId) {
        Order order = this.orderDao.findById(orderId);
        order.setPayStatus(1);
        this.orderDao.updatePay(order);
    }
    /**
     * 查出该用户的所有订单
     * @param memberId
     * @return
     */
    @Override
    public List<Order> findAllOrder(Integer memberId) {
        return this.orderDao.findAllOrder(memberId);
    }
    /**
     * 查询setmeal
     * @param setmealId
     * @return
     */
    @Override
    public Setmeal findSetmeal(Integer setmealId) {
        return this.setmealDao.findById(setmealId);

    }
    @Override
    public Result add(Map map) throws Exception {
        /**
         * TODO
         * 1. 如果选择的日期没有设置过预约, 不允许预约
         * 2. 如果选择的日期已经约满, 不允许预约
         * 3. 如果当前用户未登录, 跳转到登录页面
         * 4. 如果当前用户已经预约过该体检套餐, 不允许预约  (身份证相同即为同一用户)
         * 5. 预约成功后, 扣减可预约人数 , 跳转到预约成功, 支付页面
         */
        String phoneNumber = (String) map.get("phoneNumber");//手机号码
        String name = (String) map.get("name");//姓名
        String idCard = (String) map.get("idCard");//身份证号码
        String sex = (String) map.get("sex");//性别
        Integer addressId = (Integer) map.get("addressId");//医院id
        String setmealId = (String) map.get("setmealId");//套餐id
        String orderDate = (String) map.get("orderDate");//预约时间

        //根据手机查询会员信息
        Member member = memberDao.findByTelephone(phoneNumber);
        //更新用户信息
        member.setName(name);//名字
        member.setIdCard(idCard);//身份证号码
        member.setSex(sex);//性别
        memberDao.updateByPhone(member);

        //保存预约信息到预约表
        Date date = DateUtils.parseString2Date(orderDate);//转换为Date

        Order order = new Order();
        IdWorker idWorker = new IdWorker();
        long id = idWorker.nextId();

        order.setId(String.valueOf(id));//订单id
        order.setMemberId(member.getId());//会员id
        order.setOrderDate(date);//预约时间
        order.setOrderType(Order.ORDERTYPE_WEIXIN);//预约类型
        order.setOrderStatus(Integer.valueOf(0));//预约状态
        order.setSetmealId(Integer.valueOf(setmealId));//预约套餐id
        order.setArrival(Integer.valueOf(0));//是否到诊
        order.setPhoneNumber(phoneNumber);//手机号码
        order.setName(name);//预约人姓名
        order.setIdCard(idCard);//预约身份证
        order.setSex(sex);//性别
        order.setAddressId(addressId);//预约医院id
        order.setPayStatus(Integer.valueOf(0));//支付状态
        orderDao.add(order);

        return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
    }
}
